const multer = require("multer");
const path = require("path");
const { UPLOAD } = require("../config/config.default");

module.exports.uploadImg = multer({
  // storage: multer.diskStorage({
  //   filename: (req, file, cb) => {
  //     cb(null, Date.now() + path.extname(file.originalname));
  //   },
  //   destination: (req, file, cb) => {
  //     cb(null, uploadConfig.dir);
  //   },
  // }),
  // 先存储到内存 再转移到硬盘 避免上传后校验不通过造成的垃圾文件
  storage: multer.memoryStorage(),
  limits: {
    fileSize: UPLOAD.fileSize,
  },
  fileFilter: (req, file, cb) => {
    const filetypes = /jpeg|jpg|png/;
    const extname = filetypes.test(
      path.extname(file.originalname).toLowerCase()
    );
    const mimetype = filetypes.test(file.mimetype);

    if (mimetype && extname) {
      return cb(null, true);
    } else {
      cb("Error: Images only (JPEG, JPG, PNG)!22");
    }
  },
});
